package com.efrei.uchat.client.gui.view;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.JDialog;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTabbedPane;

import com.efrei.uchat.client.gui.control.UChatControl;

public class UChatWindow extends JDialog {

	private static final long serialVersionUID = 1317497866237686212L;
	
	private JMenu mnActions;
	private JMenuItem mntmConnexion;
	private JMenu mnConfig;
	private JMenuItem mntmInfos;
	private JTabbedPane tabbedPane;

	public UChatWindow() {
		super();
		setMinimumSize(new Dimension(500, 400));
		init();
	}
	
	private void init() {
		setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
		setTitle("UChat");
		setVisible(true);
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0};
		gridBagLayout.columnWeights = new double[]{1.0, Double.MIN_VALUE};
		gridBagLayout.rowWeights = new double[]{1.0, Double.MIN_VALUE};
		getContentPane().setLayout(gridBagLayout);
		
		tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		GridBagConstraints gbc_tabbedPane = new GridBagConstraints();
		gbc_tabbedPane.fill = GridBagConstraints.BOTH;
		gbc_tabbedPane.gridx = 0;
		gbc_tabbedPane.gridy = 0;
		getContentPane().add(tabbedPane, gbc_tabbedPane);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		mnActions = new JMenu("Actions");
		menuBar.add(mnActions);
		
		mntmConnexion = new JMenuItem("Connexion");
		mnActions.add(mntmConnexion);
		
		mnConfig = new JMenu("Config");
		menuBar.add(mnConfig);
		
		mntmInfos = new JMenuItem("Infos");
		mnConfig.add(mntmInfos);
	}
	
	public void setController(final UChatControl controller) {
		addWindowListener(new WindowAdapter() {
			
			private UChatControl control = controller;
			
			private void close() {
				if(control!=null) {
					control.exit();
					control = null;
				}
			}
			
			@Override
			public void windowClosing(WindowEvent e) {
				close();
			}
			
			@Override
			public void windowClosed(WindowEvent e) {
				close();
			}
		});
		mntmInfos.addActionListener(controller);
		mntmConnexion.addActionListener(controller);
	}
	
	public JTabbedPane getTabbedPane() {
		return tabbedPane;
	}

	public JMenuItem getMntmConnexion() {
		return mntmConnexion;
	}

	public JMenuItem getMntmInfos() {
		return mntmInfos;
	}
}
